<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 最长等差数列 -->
    <script>
      // 思路
      // 1. dp[i][diff] 表示以 nums[i] 结尾，公差为 diff 的最长等差子序列的长度
      // 2. dp[i][diff] = (dp[j][diff] || 1)+ 1, j < i, 其中diff = nums[j] - nums[i]
      // 3. 在遍历过程中取最大值
      let longestArithSeqLength = function (nums) {
        let dp = new Array(nums.length).fill().map(() => new Map())
        let max = 0
        for (let i = 1; i < nums.length; i++) {
          for (let j = 0; j < i; j++) {
            let diff = nums[i] - nums[j]
            let count = dp[j].get(diff) || 1
            dp[i].set(diff, count + 1)
            max = Math.max(max, count + 1)
          }
        }
        return max
      }
      console.log(longestArithSeqLength([3, 6, 9, 12]))
    </script>
  </body>
</html>
